﻿using DB;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TrainTicketBookingSystem
{
    internal class UserService
    {
        public int Login(string account,string password)
        {
            User u = FindUserByAccount(account);
            //return (u == null ? 0 : (u.Password == password ? 1:-1));
            if (u == null)
                return 0; // 用户不存在
            else if (u.Password == password)
            {
                if (u.RoleType == "普通用户")
                    return 1; 
                else if (u.RoleType == "管理员")
                    return 2; 
                else
                    return -1; 
            }
            else
                return -1; // 密码错误
        }

        public User FindUserByAccount(string account)
        {
            User rs = null;
            string sql = " select * from user where account = @Account limit 1 ";
            MySqlDataReader reader = MySqlUtil.ExecutQuery(sql,new MySqlParameter("@Account",account));
            if (reader.Read())
            {
                int id = reader.GetInt32("userId");
                string Account = reader.GetString("account");
                string Password = reader.GetString("password");
                string name = reader.GetString("name");
                string sex = reader.GetString("sex");
                string phonenum = reader.GetString("phonenum");
                string certificateNum = reader.GetString("certificate_num");
                string roleType = reader.GetString("roleType");
                rs = new User(id,Account,Password,name,sex,phonenum,certificateNum,roleType);
            }
            return rs;
        }

        public User FindUserByCertificateNum(string CertificateNum)
        {
            User rs = null;
            string sql = " select * from user where certificate_num = @CertificateNum limit 1 ";
            MySqlDataReader reader = MySqlUtil.ExecutQuery(sql, new MySqlParameter("@CertificateNum", CertificateNum));
            if (reader.Read())
            {
                int id = reader.GetInt32("userId");
                string Account = reader.GetString("account");
                string Password = reader.GetString("password");
                string name = reader.GetString("name");
                string sex = reader.GetString("sex");
                string phonenum = reader.GetString("phonenum");
                string certificateNum = reader.GetString("certificate_num");
                string roleType = reader.GetString("roleType");
                rs = new User(id, Account, Password, name, sex, phonenum, certificateNum, roleType);
            }
            return rs;
        }

        public void ReSetPassword(User u)
        {
            string sql = " update user set password = @Password where account = @Account ";
            MySqlUtil.ExecuteUpdate(sql, new MySqlParameter("@Password", u.Password), new MySqlParameter("@account", u.Account));
            return;
        }

        public int Regist(User u)
        {
            if(FindUserByAccount(u.Account) != null)
            {
                return 0;
            }

            if (FindUserByCertificateNum(u.CertificateNum) != null)
            {
                return -1;
            }

            AddUser(u);
            return 1;
        }

        public void AddUser(User u)
        {
            string sql = " INSERT `user`(`account`,`password`,`name`,`sex`,`phonenum`,`certificate_num`) " + 
                " VALUES (@account, @password, @name, @sex, @phonenum, @certificateNum) ";
            MySqlUtil.ExecutQuery(sql, new MySqlParameter("@account", u.Account),
                new MySqlParameter("@password", u.Password),
                new MySqlParameter("@name", u.Name),
                new MySqlParameter("@sex", u.Sex),
                new MySqlParameter("@phonenum", u.Phonenum),
                new MySqlParameter("@certificateNum", u.CertificateNum));

            return;
        }

        public int findMaxId()
        {
            string sql = " select MAX(userId) from user ";
            MySqlDataReader reader = MySqlUtil.ExecutQuery(sql);

            if (reader.Read())
            {
                return reader.GetInt32("MAX(userId)");
            }
            return 0;
        }
    }
}
